﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using UofCASE.GreenPepe.Runner;

namespace UofCASE.AgileProductLinerDSL.Nant.Tasks.Models
{
    public class Test : ITest
    {

        public FileInfo TestFile { get; private set; }
        public AspenFSProxy Owner { get; set; }

        public Test(FileInfo testFile)
        {
            TestFile = testFile;
            
        }

        public ITestKey Key
        {
            get
            {
                return new TestKey(TestFile);
            }
        }

        public bool Passed
        {
            get
            {
                if (!ResultFile.Exists)
                {
                    throw new NoResultException();
                }
                String result = File.ReadAllText(ResultFile.FullName);
                return !result.Contains("#FFAAAA;") && !result.Contains("#FFAAAA;");
            }
        }

        public bool Failed
        {
            get
            {
                return !Passed;
            }
        }

        public void Run()
        {
            UofCASE.GreenPepe.GPTest.Test test = new UofCASE.GreenPepe.GPTest.Test(new UofCASE.GreenPepe.GPTest.TestFile(TestFile.FullName, AssemblyPath));
            test.TestFile.OutputFile = ResultFile;
            GreenPepeRunner runner = new IntellitraceGreenPepeRunner(LogFile, CollectionPlan.FullName);
            UofCASE.GreenPepe.GPTest.Test res = runner.RunTest(test);
        }

        public FileInfo ResultFile
        {
            get
            {
                String path = PlatformDir;
                path += "/Results/" + Path.GetFileNameWithoutExtension(TestFile.FullName) + "Result.html";
                return new FileInfo(path);
            }
        }

        public FileInfo LogFile
        {
            get
            {
                String path = PlatformDir;
                path += "/Results/" + Path.GetFileNameWithoutExtension(TestFile.FullName) + ".aspenLog";
                return new FileInfo(path);
            }
        }

        public override int GetHashCode()
        {
            return TestFile.FullName.GetHashCode();
        }

        private string PlatformDir
        {
            get{
                return Owner.PlatformDir;
            }
        }

        public String AssemblyPath
        {
            get
            {
                return Owner.AssemblyPath;
            }
        }
        public FileInfo CollectionPlan
        {
            get
            {
                return Owner.CollectionPlan;
            }
        }
    }
}
